var PSMarkers = [];
var contextPath = '';
var LanguageConst = {
    _MSG_DELETE_POINT_SALE_CONFIRM : ''
};

var CurrentPosition = {
    lat : 21.022902,
    lng : 105.852453
};
var PointSale ={
   "id" : 0,
   "name" : "",
   "other_information" : ""
};

var Map = {
    map : null,
    geocoder :null,
    address:null,
    iconMarker:"http://www.celltreesinc.com/maps/icons/green-dot.png",
    iconOtherMarker:"http://www.celltreesinc.com/maps/icons/red-dot.png",
    currentMarker : null,
    
    onMarkerClicked:function(map,marker){},
    initMap:function() {
        var position = new google.maps.LatLng(CurrentPosition.lat,CurrentPosition.lng);
        var mapOptions = {
            center: position,
            zoom: 15,
            zIndex: null,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
     
        Map.geocoder = new google.maps.Geocoder();
        Map.map = new google.maps.Map(document.getElementById("map"), mapOptions);
        
    },
    
    setPoint:function(location){
        function codeAddress() {
            Map.geocoder.geocode( {
                'address':location
            }, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    var latlng = results[0].geometry.location;
                    Map.map.setCenter(latlng);
                    Map.createMarker(latlng);
                } 
            }
            );
        }
        codeAddress();
        return true;
    },
    
    setPosition:function(position){
        Map.map.setCenter(position);
        Map.createMarker(position);
        return true;
    },
    
    createMarker:function (position) {
        var marker = new google.maps.Marker({
            position: position,
            clickable: true,
            map: Map.map,
            icon: Map.iconMarker
        });
        google.maps.event.addListener(marker, 'click', function() {
            Map.onMarkerClicked(Map.map,marker);
        });
        Map.currentMarker = marker;
        return marker;
    }
    
};

function loadMapSize(){
    var region  = document.getElementById("region");
    var map  = document.getElementById("map");
    var bodyHeight = getBodyHeight();
    var bodyWidth = getBodyWidth();
    var navigationWidth = getWidth("navigation");
    var pointSaleWidth = getWidth("point_sale_list");
    region.style.height = (bodyHeight) +"px";
    region.style.width = (bodyWidth-navigationWidth-pointSaleWidth) +"px";
    map.style.height  = region.style.height;
}

function onPointSaleItemOver(id){
    var prefix = "control_";
    document.getElementById(prefix+id).style.display="";
}

function onPointSaleItemOut(id){
    var prefix = "control_";
    document.getElementById(prefix+id).style.display="none";
}

function deletePointSale(idObject){
    if(confirmMessage(LanguageConst._MSG_DELETE_POINT_SALE_CONFIRM)){
        location.href=contextPath+'/point-sale/deletePointSale?id='+idObject;
    }
}

function locateAddress(idObject){
    if(Map.currentMarker != null){
        Map.currentMarker.setMap(null);
    }
    var item = PSMarkers[idObject];
    var position = new google.maps.LatLng(item.position[0],item.position[1]);
    PointSale.other_information = item.other_information;
    PointSale.name = item.name;
    PointSale.id = idObject;
    Map.setPosition(position);
}

//Override
Page.load = function(){
    Navigation.hide();
    Panel.show();
    loadMapSize();
};

Setting.onSettingMouseOver = function(objMe){
    var map  = document.getElementById("region");
    map.style.zIndex = -1;
}

Setting.onSettingMouseOut = function(objMe){
    var map  = document.getElementById("region");
    map.style.zIndex = 0;
}

Page.resize = function(){
    loadMapSize();
}

Map.onMarkerClicked = function(map,marker){
    if(PointSale.name != ""){
        info = new google.maps.InfoWindow({
            content:"<h3>"+PointSale.name+"</h3><div>"+PointSale.other_information+"</div>"
        });
        info.open(map,marker);
    }
}